<?php
namespace Quality\Start\App\Action;

use \Symfony\Component\HttpFoundation\Request;
use \Symfony\Component\HttpFoundation\RedirectResponse;
use \Quality\Start\App\Service\UserControl;
use \BadMethodCallException;
use \Symfony\Component\HttpFoundation\Response;
use \Quality\Start\Application;

abstract class Action
{
    /**
     * @var \Symfony\Component\HttpFoundation\Request
     */
    protected $request;

    /**
     * @var \Symfony\Component\HttpFoundation\Response
     */
    protected $response;

    /**
     * @var \Quality\Start\Application
     */
    protected $application;

    /**
     * @var \Quality\Start\App\Service\UserControl
     */
    protected $userControl;

    /**
     * @var array
     */
    protected $params;

    protected function setAttributes(Request $request)
    {
        // echo '<pre>'; var_dump($request->attributes); die();
        $this->setRequest($request);
        $this->setApplication($request->attributes->get('application'));

        $this->userControl = $this->get('user.control');
        $this->params['loggedUser'] = $this->userControl->getLoggedUser();
        $this->params['baseUrl']= $this->getParameter('base.url');
    }

    /**
     * @param \Symfony\Component\HttpFoundation\Request $request
     */
    protected function setRequest(Request $request)
    {
        $this->request = $request;
    }

    /**
     * @return Response
     */
    public function getResponse()
    {
        if ($this->response === null) {
            $this->response = new Response();
        }

        return $this->response;
    }

    /**
     * @param \Quality\Start\Application $application
     */
    public function setApplication(Application $application)
    {
        $this->application = $application;
    }

    /**
     * @param string $serviceId
     * @return mixed
     * @throws \BadMethodCallException
     */
    protected function get($serviceId)
    {

        if ($this->application->getDependencyContainer() === null) {
            throw new BadMethodCallException(
                'The dependency container must be defined'
            );
        }

        return $this->application->getDependencyContainer()->get($serviceId);
    }

    /**
     * @param string $paramId
     * @return mixed
     * @throws \BadMethodCallException
     */
    protected function getParameter($paramId)
    {
        if ($this->application->getDependencyContainer() === null) {
            throw new BadMethodCallException(
                'The dependency container must be defined'
            );
        }

        return $this->application->getDependencyContainer()->getParameter($paramId);
    }

    /**
     * @param string $path
     * @param boolean $interrupt
     */
    protected function forward($path, $interrupt = false)
    {
        $this->application->forward($path, $interrupt);
    }

    /**
     * @param string $url
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     */
    protected function redirect($url)
    {
        return new RedirectResponse($url);
    }

    /**
     * @param string $template
     */
    protected function render($template)
    {
        return $this->getResponse()->setContent($this->get('twig')->render($template, $this->params));
    }
}
